Skip to content

feat: Implement sql.Scanner interface#24

Merged
freeformz merged 7 commits intomainfrom
ffz/Scanner
Jun 8, 2025
Merged

feat: Implement sql.Scanner interface#24
freeformz merged 7 commits intomainfrom
ffz/Scanner

Conversation

@freeformz
Copy link
Owner

@freeformz freeformz commented Jun 8, 2025

This should allow using this with database/sql better.

@freeformz freeformz requested a review from Copilot June 8, 2025 22:21

This comment was marked as outdated.

@freeformz freeformz requested a review from Copilot June 8, 2025 22:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for scanning JSON-encoded sets from a database by implementing the sql.Scanner interface across all set types.

  • Introduces a shared scanValue helper and Scan methods for Map, Ordered, SyncMap, Locked, and LockedOrdered.
  • Refines Clear and UnmarshalJSON logic to ensure proper initialization of internal maps and slices.
  • Updates README to correct the package name reference.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
sync.go Added Scan implementation for SyncMap.
ordered.go Improved Clear init logic, refactored UnmarshalJSON, added Scan.
map.go Enhanced Clear, added scanValue helper, implemented Scan.
locked_ordered.go Added Scan implementation for LockedOrdered.
locked.go Added Scan implementation for Locked.
README.MD Corrected package name from set to sets.
Comments suppressed due to low confidence (5)

map.go:149

  • The new scanValue helper and associated Scan methods aren’t covered by unit tests. Add test cases to verify scanning nil, []byte, and string inputs into each set type.
func scanValue[M comparable](src any, clear func() int, unmarshal func([]byte) error) error {

ordered.go:53

  • [nitpick] Clearing the index map by deleting each key can be slow for large maps. Consider resetting the map directly (e.g., s.idx = make(map[M]int)) for faster clears.
for k := range s.idx {

map.go:52

  • [nitpick] Clearing the set map by iterating and deleting keys can be expensive. Reinitializing the map (e.g., s.set = make(map[M]struct{})) might yield better performance.
for k := range s.set {

map.go:158

  • The Scan helper only accepts []byte or string. To fully satisfy sql.Scanner, consider handling common DB types such as sql.NullString, sql.RawBytes, or implementing driver.Valuer support.
default:

map.go:159

  • [nitpick] The error for unsupported source types could be clearer by indicating that a JSON array is expected, e.g., "cannot scan set: expected JSON array (as []byte or string), got %T".
return fmt.Errorf("cannot scan set of type %T - not []byte or string", st)


// scanValue is a helper function that implements the common logic for scanning values into sets.
// It handles nil, []byte, and string types, delegating to the provided unmarshal function.
func scanValue[M comparable](src any, clear func() int, unmarshal func([]byte) error) error {
Copy link

Copilot AI Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The clear callback returns an int that scanValue ignores. Consider changing it to a func() signature to avoid confusion over the unused return value.

Copilot uses AI. Check for mistakes.
@freeformz freeformz merged commit e7d1739 into main Jun 8, 2025
9 checks passed
@freeformz freeformz deleted the ffz/Scanner branch June 8, 2025 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant